;; init-dict.el --- Initialize dictionaries.	-*- lexical-binding: t -*-

;; Copyright (C) 2021 Vincent Zhang

;; Author: Vincent Zhang <seagle0128@gmail.com>
;; URL: https://github.com/seagle0128/.emacs.d

;; This file is not part of GNU Emacs.
;;
;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
;; published by the Free Software Foundation; either version 3, or
;; (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;; General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program; see the file COPYING.  If not, write to
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth
;; Floor, Boston, MA 02110-1301, USA.
;;

;;; Commentary:
;;
;; Multiple dictionaries.
;;

;;; Code:

(eval-when-compile
  (require 'init-const))

;; A multi dictionaries interface
(use-package fanyi
  :bind (("C-c d f" . fanyi-dwim)
         ("C-c d d" . fanyi-dwim2)
         ("C-c d h" . fanyi-from-history)))

(when emacs/>=28p
  (use-package go-translate
    :bind (("C-c g"   . gt-do-translate)
           ("C-c G"   . gt-do-translate-prompt)
           ("C-c u"   . gt-do-text-utility)
           ("C-c d g" . gt-do-translate)
           ("C-c d G" . gt-do-translate-prompt)
           ("C-c d p" . gt-do-speak)
           ("C-c d s" . gt-do-setup)
           ("C-c d u" . gt-do-text-utility))
    :init
    (setq gt-langs '(en zh)
          gt-buffer-render-follow-p t
          gt-buffer-render-window-config
          '((display-buffer-reuse-window display-buffer-in-direction)
            (direction . bottom)
            (window-height . 0.4)))

    (setq gt-pop-posframe-forecolor (face-foreground 'tooltip nil t)
          gt-pop-posframe-backcolor (face-background 'tooltip nil t))
    (when (facep 'posframe-border)
      (setq gt-pin-posframe-bdcolor (face-background 'posframe-border nil t)))
    :config
    (with-no-warnings
      (setq gt-preset-translators
            `((default . ,(gt-translator
                           :taker   (list (gt-taker :pick nil :if 'selection)
                                          (gt-taker :text 'paragraph :if '(Info-mode help-mode helpful-mode devdocs-mode))
                                          (gt-taker :text 'buffer :pick 'fresh-word
                                                    :if (lambda (translatror)
                                                          (and (not (derived-mode-p 'fanyi-mode)) buffer-read-only)))
                                          (gt-taker :text 'word))
                           :engines (if (display-graphic-p)
                                        (list (gt-bing-engine :if 'not-word)
                                              (gt-youdao-dict-engine :if 'word))
                                      (list (gt-bing-engine :if 'not-word)
                                            (gt-youdao-dict-engine :if 'word)
                                            (gt-youdao-suggest-engine :if 'word)
                                            (gt-google-engine :if 'word)))
                           :render  (list (gt-posframe-pop-render
                                           :if (lambda (translator)
                                                 (and (display-graphic-p)
                                                      (not (derived-mode-p 'Info-mode 'help-mode 'helpful-mode 'devdocs-mode))
                                                      (not (member (buffer-name) '("COMMIT_EDITMSG")))))
                                           :frame-params (list :accept-focus nil
                                                               :width 70
                                                               :height 15
                                                               :left-fringe 16
                                                               :right-fringe 16
                                                               :border-width 1
                                                               :border-color gt-pin-posframe-bdcolor))
                                          (gt-overlay-render :if 'read-only)
                                          (gt-insert-render :if (lambda (translator) (member (buffer-name) '("COMMIT_EDITMSG"))))
                                          (gt-buffer-render))))
              (multi-dict . ,(gt-translator :taker (gt-taker :prompt t)
                                            :engines (list (gt-bing-engine)
                                                           (gt-youdao-dict-engine)
                                                           (gt-youdao-suggest-engine :if 'word)
                                                           (gt-google-engine))
                                            :render (gt-buffer-render)))
              (Text-Utility . ,(gt-text-utility :taker (gt-taker :pick nil)
                                                :render (gt-buffer-render)))))

      (defun gt--do-translate (dict)
        "Translate using DICT from the preset tranlators."
        (gt-start (alist-get dict gt-preset-translators)))

      (defun gt-do-translate-prompt ()
        "Translate with prompt using the multiple dictionaries."
        (interactive)
        (gt--do-translate 'multi-dict))

      (defun gt-do-text-utility ()
        "Handle the texts with the utilities."
        (interactive)
        (gt--do-translate 'Text-Utility)))))

;; OSX dictionary
(when sys/macp
  (use-package osx-dictionary
    :bind (("C-c d i" . osx-dictionary-search-input)
           ("C-c d x" . osx-dictionary-search-pointer))))

(provide 'init-dict)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; init-dict.el ends here
